package com.jlc.book.shop.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jlc.book.shop.delegate.BookDelegate;
import com.jlc.book.shop.to.BookTO;
import com.jlc.book.shop.validator.JLCDataValidator;

public class AddBookAction {
	public String addBookInfo(HttpServletRequest req,HttpServletResponse res){
		String page="addBookDef.jsp";
		String bnm=req.getParameter("bname");
		String author=req.getParameter("author");
		String cost=req.getParameter("cost");
		String pub=req.getParameter("publication");
		String edi=req.getParameter("edition");
		float bcost=0.0f;
		boolean convertCost=true;
		boolean intractWithDB=true;
		/*VALIDATING BOOK NAME*/
		if(!JLCDataValidator.validateRequired(bnm)){
			req.setAttribute("bname","Book name is required");
			intractWithDB=false;
		}else if(!JLCDataValidator.minLength(bnm, 3)){
			req.setAttribute("bname","Book name must be minimum 3 charater long");
			intractWithDB=false;
		}else if(!JLCDataValidator.maxLength(bnm, 60)){
			req.setAttribute("bname","Book name must be maximum 60 charater long");
			intractWithDB=false;
		}
		/*VALIDATING AUTHOR NAME*/
		if(!JLCDataValidator.validateRequired(author)){
			req.setAttribute("author","Author name is required");
			intractWithDB=false;
		}else if(!JLCDataValidator.minLength(author, 3)){
			req.setAttribute("author","Author name must be minimum 3 charater long");
			intractWithDB=false;
		}else if(!JLCDataValidator.maxLength(author, 50)){
			req.setAttribute("author","Author name must be maximum 50 charater long");
			intractWithDB=false;
		}
		if(!JLCDataValidator.validateRequired(cost)){
			req.setAttribute("cost","Cost is required");
			intractWithDB=false;
			convertCost=false;
		}
		if(!JLCDataValidator.validateRequired(edi)){
			req.setAttribute("edition","Edition is required");
			intractWithDB=false;
		}
		/*VALIDATING AUTHOR PUBLICATION*/
		if(!JLCDataValidator.validateRequired(pub)){
			req.setAttribute("publication","Publication is required");
			intractWithDB=false;
		}else if(!JLCDataValidator.minLength(pub, 3)){
			req.setAttribute("publication","Publication name must be minimum 3 charater long");
			intractWithDB=false;
		}else if(!JLCDataValidator.maxLength(pub, 50)){
			req.setAttribute("publication","Publication name must be maximum 50 charater long");
			intractWithDB=false;
		}
		if(convertCost){
			try{
				bcost=Float.parseFloat(cost);
			}catch(NumberFormatException e){
				req.setAttribute("cost","Cost is not valid");
				intractWithDB=false;
			}
		}
		if(intractWithDB){
			BookTO bto=new BookTO(bnm,author,pub,edi,bcost);
			if(BookDelegate.alreadyExist(bto)){
			req.setAttribute("addingBookError","Book information already available");
		}else{
			boolean added=BookDelegate.addBook(bto);
		if(added){
			req.setAttribute("addingBookError","Book information added successfully");
			req.setAttribute("SHOW_ADD_BOOK","OK");
		}else{
			req.setAttribute("addingBookError","Error in adding book information");
		}
	  }
	}
		return page;
  }
}
